commonlibsse_ng\re\n/
NiTStringMap.rs

1//! # NiTStringMap
2//!
3//! This module defines the `NiTStringMap<T>` and `NiTStringTemplateMap<Parent, T>` structs,
4//! simulating the base map and its template inheritance behavior.
5
6use crate::re::NiTMap::NiTMap;
7use core::{ffi::c_char, fmt};
8
9/// Represents a string template map.
10///
11/// Simulates the `NiTStringTemplateMap` C++ class.
12///
13/// - `V`: Map value type
14#[repr(C)]
15#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
16pub struct NiTStringTemplateMap<Parent, V> {
17    /// Base map.
18    pub __base: Parent,
19
20    /// Copy flag.
21    pub copy: bool,
22
23    // Padding to align with C++ memory layout.
24    pub pad21: u8,
25    pub pad22: u16,
26    pub pad24: u32,
27
28    marker: core::marker::PhantomData<V>,
29}
30
31const _: () = {
32    type ParentType = [u8; 0x20];
33    assert!(core::mem::offset_of!(NiTStringTemplateMap::<ParentType, ()>, __base) == 0x00);
34    assert!(core::mem::offset_of!(NiTStringTemplateMap::<ParentType, ()>, copy) == 0x20);
35    assert!(core::mem::offset_of!(NiTStringTemplateMap::<ParentType, ()>, pad21) == 0x21);
36    assert!(core::mem::offset_of!(NiTStringTemplateMap::<ParentType, ()>, pad22) == 0x22);
37    assert!(core::mem::offset_of!(NiTStringTemplateMap::<ParentType, ()>, pad24) == 0x24);
38    assert!(core::mem::size_of::<NiTStringTemplateMap::<ParentType, ()>>() == 0x28);
39};
40
41/// Represents a case-sensitive string map.
42///
43/// - `V`: Map value type
44#[repr(C)]
45#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
46pub struct NiTStringMap<V> {
47    pub __base: NiTStringTemplateMap<NiTMap<*const c_char, V>, V>,
48}
49const _: () = {
50    assert!(core::mem::offset_of!(NiTStringMap::<()>, __base) == 0x00);
51    assert!(core::mem::size_of::<NiTStringMap::<()>>() == 0x28);
52};
53
54impl<V: fmt::Debug> fmt::Debug for NiTStringMap<V> {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        let mut map = f.debug_map();
57        for (key, value) in self.__base.__base.iter() {
58            let key_str = unsafe {
59                if key.is_null() {
60                    "<null>"
61                } else {
62                    core::ffi::CStr::from_ptr(*key).to_str().unwrap_or("<invalid utf8>")
63                }
64            };
65            map.entry(&key_str, value);
66        }
67        map.finish()
68    }
69}